/*素材管理*/
import request from '@/utils/request';
import { object2Url } from '@/utils/trans';

/**
 * 获取用户oss配置数据
 */
export async function userOssList(data) {
  const res = await request.post('/business/aliyunoss/userOssList', data);
  if (res.data.code === 2000) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

export async function csApi22(data) {
  const res = await request.post('/business/matrixsphpublish/sphQrCode', data);
  if (res.data.code === 2000) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 视频号发布——二维码链接生成
export async function csApi() {
  const ts = new Date().getTime();
  const ua = navigator.userAgent;
  const us = {
    ts,
    ua
  };
  let url = `/business/matrixsphpublish/sphQrCode${object2Url(us)}`;
  const res = await request.get(url, us);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 视频号二维码校检
export async function sphQrStatus(token) {
  const ts = new Date().getTime();
  const ua = navigator.userAgent;
  const us = {
    ts,
    ua,
    token: token
  };
  let url = `/business/matrixsphpublish/sphQrStatus${object2Url(us)}`;
  const res = await request.get(url, us);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 获取文件上传签名数据,商家端
 */
export async function userOssSignature() {
  const ts = new Date().getTime();
  const ua = navigator.userAgent;
  const us = {
    ts,
    ua
  };
  let url = `/business/aliyunoss/userOssSignature${object2Url(us)}`;
  const res = await request.get(url, us);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 获取文件上传签名数据,oem端
 */
export async function oemOssSignature() {
  const ts = new Date().getTime();
  const ua = navigator.userAgent;
  const us = {
    ts,
    ua
  };
  let url = `/business/aliyunoss/oemOssSignature${object2Url(us)}`;
  const res = await request.get(url, us);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 获取文件上传签名数据,主平台端
 */
export async function mainOssSignature() {
  const ts = new Date().getTime();
  const ua = navigator.userAgent;
  const us = {
    ts,
    ua
  };
  let url = `/business/aliyunoss/mainOssSignature${object2Url(us)}`;
  const res = await request.get(url, us);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 查询用户文件夹列表
 */
export async function listUserFiles(params) {
  const res = await request.post('/business/montage/folder/all', params);
  if (res.data.code === 2000 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 获取md5加密加盐参数
 */
export async function publicFile() {
  const ts = new Date().getTime();
  const ua = navigator.userAgent;
  const us = {
    ts,
    ua
  };
  let url = `/business/montage/folder/common${object2Url(us)}`;
  const res = await request.get(url, us);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 创建文件夹
 */
export async function addUserFile(data) {
  const res = await request.post('/business/montage/folder/save', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改文件夹
 */
export async function updateUserFile(data) {
  const res = await request.post('/business/montage/folder/update', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}
/**
 * 批量删除文件夹（单个删除通用）
 */
export async function batchDelete(data) {
  const res = await request.post('/business/montage/folder/batchDelete', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 视频文件上传回调
export async function videoUpoloadCB(data) {
  const res = await request.post(
    '/business/montage/media/videoUpoloadCB',
    data
  );
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 音频文件上传回调
export async function audioUploadCB(data) {
  const res = await request.post('/business/montage/media/audioUploadCB', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

// url链接文件直接上传阿里云，返回阿里云url地址。
// 暂只有混剪——热榜音乐，和摄像头‘自动成片’，‘采集素材’使用）
export async function ossUpdate(fileUrl) {
  const ts = new Date().getTime();
  const ua = navigator.userAgent;
  const us = {
    url: fileUrl, // 上传文件url
    videoType: 'mp3', // 上传文件类型
    ts,
    ua
  };
  let url = `/business/cameraMedia/ossUpdate${object2Url(us)}`;
  const res = await request.get(url, us);
  if (res.data.code === 2000) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 数字人多传username字段，因此加
// 暂只有混剪——热榜音乐，和摄像头‘自动成片’，‘采集素材’使用）
export async function ossUpdate2(fileUrl, username) {
  const ts = new Date().getTime();
  const ua = navigator.userAgent;
  const us = {
    url: fileUrl, // 上传文件url
    videoType: 'mp3', // 上传文件类型
    username: username,
    ts,
    ua
  };
  let url = `/business/cameraMedia/ossUpdate${object2Url(us)}`;
  const res = await request.get(url, us);
  if (res.data.code === 2000) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 成片管理，新——保存视频到指定文件夹
export async function saveVideoFile(data) {
  const res = await request.post('/business/montage/projectjob/save', data);
  if (res.data.code === 2000) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 媒资注册并保存到对应文件夹
export async function registerMedia(data) {
  const res = await request.post('/business/montage/media/registerMedia', data);
  if (res.data.code === 2000) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 媒资注册，不保存到文件夹
export async function registerAssetsMedia(data) {
  const res = await request.post('/business/cameraMedia/registerMedia', data);
  if (res.data.code === 2000) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 分页获取文件夹文件(二选一）_暂未使用
 * @param params 查询条件
 */
export async function mediaPage(params) {
  const res = await request.get('/business/montage/media/page', {
    params
  });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 根据条件获取所有文件(二选一：当前所用）
export async function getFileAll(data) {
  const res = await request.post('/business/montage/media/all', data);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 批量修改文件所在文件夹【单个移动通用】
export async function updateFolder(data) {
  const res = await request.post('/business/montage/media/updateFolder', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 批量删除文件【真实删除】【媒资】
export async function batchDeleteFile(data) {
  const res = await request.post('/business/montage/media/batchDelete', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 解析SRT字幕文件
export async function resolveSRT(data) {
  const res = await request.post('/business/montage/srt/resolveSRT', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 音频台本转换成文本台本
export async function convertAudioScript(data) {
  const res = await request.post(
    '/business/montage/script/convertAudioScript',
    data
  );
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}
// 根据条件获取所有台本
export async function getTxtAll(data) {
  const res = await request.post('/business/montage/script/all', data);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

//批量删除台本【真实删除】
export async function batchTxtDelete(data) {
  const res = await request.post('/business/montage/script/batchDelete', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 保存台本
export async function saveText(data) {
  const res = await request.post('/business/montage/script/save', data);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}
//修改台本
export async function updateText(data) {
  const res = await request.post('/business/montage/script/update', data);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

//根据ID获取台本数据
export async function loadById(data) {
  const res = await request.post('/business/montage/script/loadById', data);
  if (res.data.code === 2000) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}
